const app = getApp()

var amapFile = require('../../libs/amap-wx.130');//如：..­/..­/libs/amap-wx.js

import {
  getListRequest
} from '../../api/user'

Page({

  /** 页面的初始数据 */
  data: {
    pageNo: 1,
    pageSize: 10,
    dataArr: [],
    bottomIsLoadingHidden: false,
    isLoadingText: '加载中...',
    // success_users: [{"avatar": "../../images/icon/notice.png", "desc": "三分钟前孙漂亮**成功团购10元话费"},
    // {"avatar": "../../images/icon/notice.png", "desc": "四分钟前孙漂亮四分钟前孙漂亮四分钟前孙漂亮四分钟前孙漂亮**成功团购10元话费"},
    // {"avatar": "../../images/icon/notice.png", "desc": "五分钟前孙漂亮**成功团购10元话费"},],
    selectArray: ["宿迁市","西安市","沭阳县"],
    cityName: app.globalData.locationInfo.city,
    // cityName: app.globalData.locationInfo.city.length > 0 ? app.globalData.locationInfo.city : '建议打开定位',
    isEmptyData:false
  },

  onLoad: function (option) {
    console.log(">>>>>>>>>>>>>>homeIndex onLoad<<<<<<<<<<<<")
    console.log(">>>>>>>>>>>>>>homeIndex onLoad<<<<<<<<<<<<")
    console.log(">>>>>>>>>>>>>>homeIndex onLoad<<<<<<<<<<<<")
    console.log(">>>>>>>>>>>>>>homeIndex onLoad<<<<<<<<<<<<")
    console.log(">>>>>>>>>>>>>>homeIndex onLoad<<<<<<<<<<<<")
    console.log(">>>>>>>>>>>>>>homeIndex onLoad<<<<<<<<<<<<")
    console.log("that.globalData.locationInfo")
    console.log(app.globalData.locationInfo)
    console.log(app.globalData.locationInfo.length)
    console.log(app.globalData.locationInfo.city)
    tt.showLoading({
      title: '加载中', // 内容
    });

    /** 初始数据请求 */
    this.data.pageNo = 1
    this.loadMore()

    /** 通过分享点击进来的 */
    if (option.hasOwnProperty('shareUser')) {
      app.globalData.shareUser = option.shareUser
    }
    
  },
  
  /* 下拉组件 */
  selectEvent: function (e) {
      console.log(e.detail.selectedData)
      console.log(app.globalData.locationInfo.district) 
      tt.showLoading({
        title: '加载中', // 内容
      });
      this.setData({
        cityName: e.detail.selectedData
      })
      let that = this
      that.setData({
        pageNo: 1,
        dataArr: []
      })
      that.loadMore()
  },

  itemOnClick: function (e) {
    tt.navigateTo({
      url: `../goodsDetail/goodsDetail?id=${e.currentTarget.dataset.item.id}`,
    });
    console.log(app.globalData.locationInfo)
  },

  /** 页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {
    let that = this
    tt.startPullDownRefresh({
      success: function (res) {
        that.setData({
          pageNo: 1,
          dataArr: []
        })
        that.loadMore()
      }
    });
  },

  /** 页面上拉触底事件的处理函数 */
  onReachBottom: function () {
    console.log('onReachBottom')
    let pageIndex = this.data.pageNo;
    this.setData({
      pageNo: pageIndex + 1
    })
    this.loadMore();
  },

  /** 获取数据 */
  loadMore: function () {
    let params = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
      type: 0,  // 本地美食 休闲娱乐 生活服务 周边景点 分别对应0,1,2,3,4
      area: this.data.cityName
    };
    getListRequest(params).then((res) => {

      let list = (res.result && res.result.data) || [];
      this.setData({
        isLoadingText: list.length > 0 ? '加载中...' : '没有数据啦~'
      })
      if (this.data.pageNo > 1) {
        //追加数据
        this.setData({
          dataArr: this.data.dataArr.concat(list)
        })
      } else {
        this.setData({ dataArr: list });
        //隐藏导航条加载动画
        // tt.hideNavigationBarLoading();
        //停止下拉刷新
        tt.stopPullDownRefresh();
      }
      // if (res.result.pageNum >= res.result.pages) {
      //   this.finished = true;
      //   this.finishedText = "- 没有更多了-";
      // }
      // if (res.result.pages > this.pageNo) {
      //   this.pageNo++;
      // }
      // this.isLoading = false;

      this.bottomIsLoadingHidden = this.data.dataArr.length == this.data.pageSize || this.data.dataArr.length == 0;

      tt.hideLoading();
      this.setData({
        isEmptyData: this.data.dataArr.length > 0
      })
    }).catch((res) => {
      console.log('getListRequest请求失败')
      console.log(res)
      tt.hideLoading();
    })
  },


  SudokuAction: function (event) {
    var index = event.currentTarget.dataset.id;
    tt.navigateTo({
      url: `../sortGroup/sortGroup?type=${index}`,
    });
  },

  /** 获取用户地理位置 */
  // getLocationInfo: function () {
  //   let that = this
  //   var myAmapFun = new amapFile.AMapWX({key:'9d7e516361cc1fa3b2e8102f7b57a12e'});
  //   myAmapFun.getRegeo({
  //     success: function(locationInfo){
  //       //成功回调
  //       console.log("位置成功回调")
  //       console.log(locationInfo)
  //       that.setData({
  //         cityName: locationInfo[0].regeocodeData.addressComponent.city
  //       })
  //     },
  //     fail: function(info){
  //       //失败回调
  //       console.log("位置失败回调")
  //       console.log(info)
  //     }
  //   })
  // },

  onShareAppMessage(option) {
    return {
      title: '贝优阁',
      desc: '这是默认的转发文案，用户可以直接发送，也可以在发布器内修改',
      path: `/pages/homeIndex/homeIndex?shareUser=${tt.getStorageSync('userId')}`, // ?后面的参数会在转发页面打开时传入onLoad方法
      imageUrl: '/images/icon/logo.png',
      templateId: '这是开发者后台设置的分享素材模板id',
      success() {
        console.log('转发发布器已调起，并不意味着用户转发成功，微头条不提供这个时机的回调');
      },
      fail() {
        console.log('转发发布器调起失败');
      }
    }
  },

})
